package models;

import play.data.Upload;
import play.data.binding.TypeBinder;
import play.mvc.Http;

import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.util.List;

/**
 * Created by Gipxy group
 * Date: 3/17/12
 */

public class MyByteArrayBinder implements TypeBinder<byte[]> {
    @SuppressWarnings("unchecked")
    public Object bind(String name, Annotation[] annotations, String
            value, Class actualClass, Type genericType) throws Exception {
        List<Upload> uploads = (List<Upload>)
                Http.Request.current().args.get("__UPLOADS");

        for(Upload upload : uploads) {
            if(upload.getFieldName().equals(value)) {
                byte[] data = upload.asBytes();
                if(data.length > 0)     {
                    System.out.println("ccccc=" + data.length + "| " + data);
                    return data;
                }
            }
        }

        return "!!!!!!!!!!!!!".getBytes();

        //return Binder.MISSING;
    }
}